package com.device.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.device.Entity.Device;
import com.device.Entity.Lab;
import com.device.Exception.BaseException;
import com.device.Mapper.DeviceMapper;
import com.device.Mapper.LabMapper;
import com.device.Service.DeviceService;
import com.device.Service.LabService;
import com.device.common.PageResult;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DeviceServiceImpl  extends ServiceImpl<DeviceMapper, Device> implements DeviceService {
    @Resource
    private DeviceMapper deviceMapper;
    @Resource
    private LabMapper labMapper;
    public  void adddevice(Device device){
        LambdaQueryWrapper<Device> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Device::getNums,device.getNums());
        Device device1=deviceMapper.selectOne(lambdaQueryWrapper);
        if(device1 != null){
            throw new BaseException("当前设备编号已存在");
        }
        deviceMapper.insert(device);
    }
    public PageResult getpage(Integer pageNo, Integer pageSize, String keyword){
        PageResult pageResult=new PageResult();
        IPage<Device> page=new Page<>(pageNo,pageSize);
        IPage<Device> list=deviceMapper.getpage(page,keyword);
        pageResult.setRecords(list.getRecords());
        pageResult.setTotal(list.getTotal());
        return pageResult;


    }
    public void editdevice(Device device){
        LambdaQueryWrapper<Device> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Device::getNums,device.getNums());
        lambdaQueryWrapper.notIn(Device::getId,device.getId());
        Device device1=deviceMapper.selectOne(lambdaQueryWrapper);
        if(device1 != null){
            throw new BaseException("当前设备编号已存在");
        }
        deviceMapper.updateById(device);
    }
    public  PageResult getpage1(Integer uid, Integer pageNo, Integer pageSize, String keyword){
        PageResult pageResult=new PageResult();
        IPage<Device> page=new Page<>(pageNo,pageSize);

        IPage<Device> list=deviceMapper.getpage1(page,keyword,uid);
        pageResult.setRecords(list.getRecords());
        pageResult.setTotal(list.getTotal());
        return pageResult;
    }
}
